﻿/**
 * Copyright (c) 2008 Justin Etheredge http://www.codethinked.com
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Microsoft Public License (Ms-PL)
 * which accompanies this distribution, and is available at
 * http://www.opensource.org/licenses/ms-pl.html
 * If redistributing this code, this entire header must remain intact.
 * 
 * Some of the code in this application has been taken 
 * from the following sources (all of which are licensed under
 * the Ms-PL):
 * Matt Warren's Blog: http://blogs.msdn.com/mattwar/
 * MSDN: http://msdn2.microsoft.com/en-us/library/bb546158.aspx
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LinqToSimpleDB
{
  public sealed class SimpleDBAttributeCollection : IDirtyTrackable, IEnumerable<SimpleDBAttribute>
  {
    private Dictionary<string, SimpleDBAttribute> _attributeDictionary = null;

    private Dictionary<string, SimpleDBAttribute> AttributeDictionary
    {
      get
      {
        if (_attributeDictionary == null)
        {
          _attributeDictionary = new Dictionary<string, SimpleDBAttribute>();
        }
        return _attributeDictionary;
      }
    }

    public IEnumerable<SimpleDBAttribute> Items
    {
      get
      {
        return this.AttributeDictionary.Values;
      }
    }

    public SimpleDBAttribute this[string name]
    {
      get
      {
        return this.AttributeDictionary[name];
      }
    }

    public int Count
    {
      get
      {
        return this.AttributeDictionary.Count;
      }
    }

    public void Add(SimpleDBAttribute attribute)
    {
      if (!this.AttributeDictionary.ContainsKey(attribute.Name))
      {
        this.AttributeDictionary.Add(attribute.Name, attribute);
      }
    }

    public bool Remove(SimpleDBAttribute attribute)
    {
      return this.AttributeDictionary.Remove(attribute.Name);
    }

    public bool Contains(string attributeName)
    {
      return this.AttributeDictionary.ContainsKey(attributeName);
    }

    public bool Contains(SimpleDBAttribute attribute)
    {
      return this.AttributeDictionary.ContainsValue(attribute);
    }

    public void Clear()
    {
      this.AttributeDictionary.Clear();
    }

    #region IDirtyTrackable Members

    bool LinqToSimpleDB.IDirtyTrackable.IsDirty
    {
      get
      {
        foreach (IDirtyTrackable item in this.AttributeDictionary.Values)
        {
          if (item.IsDirty)
          {
            return true;
          }
        }
        return false;
      }
    }

    void LinqToSimpleDB.IDirtyTrackable.MarkClean()
    {
      //ignore this, it doesn't mean anything on the collection
    }

    void LinqToSimpleDB.IDirtyTrackable.MarkDirty()
    {
      //ignore this, it doesn't mean anything on the collection
    }

    #endregion

    #region IEnumerable<SimpleDBAttribute> Members

    IEnumerator<SimpleDBAttribute> IEnumerable<SimpleDBAttribute>.GetEnumerator()
    {
      foreach (SimpleDBAttribute attribute in this.AttributeDictionary.Values)
      {
        yield return attribute;
      }
    }

    #endregion

    #region IEnumerable Members

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
      foreach (SimpleDBAttribute attribute in this.AttributeDictionary.Values)
      {
        yield return attribute;
      }
    }

    #endregion
  }
}
